<?php
# @Author: yongleicheng <jetox>
# @Date:   2017-11-21T02:58:02+08:00
# @Email:  admin@weiqisheng.cn
# @Project: WQSCMS
# @Filename: Login.php
# @Last modified by:   jetox
# @Last modified time: 2017-11-21T07:29:52+08:00
# @Copyright: 微企胜网络科技有限公司




namespace app\admin\controller;

use think\Controller;
use app\admin\model\Admin;

class Login extends Controller
{
    public function _initialize()
    {
        if (session('aid')) {
            $this->redirect('index/index');
        }
    }
    private $cache_model;
    private $system;
    public function index()
    {
        if (request()->isPost()) {
            $admin = new Admin();
            $data = input('post.');
            if (!$this->check($data['captcha'])) {
                return json(array('code' => 0, 'msg' => '验证码错误'));
            }
            $num = $admin->login($data);
            if ($num == 1) {
                return json(['code' => 1, 'msg' => '登录成功!', 'url' => url('index/index')]);
            } else {
                return json(array('code' => 0, 'msg' => '用户名或者密码错误，重新输入!'));
            }
        } else {
            $this->cache_model=array('Module','Role','Category','Posid','Field','System');
            $this->system = F('System');
            if (empty($this->system)) {
                foreach ($this->cache_model as $r) {
                    savecache($r);
                }
            }
            return $this->fetch();
        }
    }
    public function check($code)
    {
        return captcha_check($code);
    }
}
